home *** CD-ROM | disk | FTP | other *** search
/ Visual Basic Source Code / Visual Basic Source Code.iso / vbsource / vbdatabs / twalk.h < prev    next >
C/C++ Source or Header  |  1999-03-14  |  4KB  |  94 lines

  1. // ------------------------------- //
  2. // -------- Start of File -------- //
  3. // ------------------------------- //
  4. // ----------------------------------------------------------- //
  5. // C++ Header File Name: twalk.h
  6. // Compiler Used: MSVC40, DJGPP 2.7.2.1, GCC 2.7.2.1, HP CPP 10.24
  7. // Produced By: Doug Gaer   
  8. // File Creation Date: 01/23/1997 
  9. // Date Last Modified: 03/15/1999
  10. // Copyright (c) 1997 Douglas M. Gaer
  11. // ----------------------------------------------------------- // 
  12. // ---------- Include File Description and Details  ---------- // 
  13. // ----------------------------------------------------------- // 
  14. /*
  15. The VBD C++ classes are copyright (c) 1997, by Douglas M. Gaer.
  16. All those who put this code or its derivatives in a commercial
  17. product MUST mention this copyright in their documentation for
  18. users of the products in which this code or its derivative
  19. classes are used. Otherwise, you have the freedom to redistribute
  20. verbatim copies of this source code, adapt it to your specific
  21. needs, or improve the code and release your improvements to the
  22. public provided that the modified files carry prominent notices
  23. stating that you changed the files and the date of any change.
  24.  
  25. THIS SOFTWARE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND.
  26. THE ENTIRE RISK OF THE QUALITY AND PERFORMANCE OF THIS SOFTWARE
  27. IS WITH YOU. SHOULD ANY ELEMENT OF THIS SOFTWARE PROVE DEFECTIVE,
  28. YOU WILL ASSUME THE COST OF ALL NECESSARY SERVICING, REPAIR, OR
  29. CORRECTION.
  30.  
  31. The TreeWalkerb class defines a generic iterator used to walk
  32. through (R)ed (B)lack binary search trees. All of the member
  33. functions are accessed via pointers to the member functions.
  34. */
  35. // ----------------------------------------------------------- //   
  36. #ifndef __TWALK_HPP
  37. #define __TWALK_HPP
  38.  
  39. #include "queue.h"   // List based stack queue
  40. #include "rbnode.h"  // Red Black tree node
  41. #include "bnode.h"   // Binary tree node
  42.  
  43. enum WalkOrder {
  44.   PREORDER,  // Visit Root first, then Left, then Right subtree recursively  
  45.   POSTORDER, // Visit Left subtree first, then Right, then Root recursively
  46.   INORDER,   // Visit Left subtree first, then Root, then Right recursively
  47.   LVLORDER   // Visit (L)evel by (L)evel, start at Root, and go Left to Right
  48. };
  49.  
  50. // (T)ree (W)alker (B)ase class
  51. class TreeWalkerb  
  52. {
  53. public:
  54.   TreeWalkerb(BNodeBase *r, WalkOrder w) { Reset(r, w); }
  55.   virtual ~TreeWalkerb() { }
  56.  
  57. public:
  58.   void Reset() { Reset(root, worder); }
  59.   void Reset(BNodeBase *r, WalkOrder w);
  60.   BNodeBase *Next() { return (this->*NextFptr)(); }
  61.  
  62. protected:
  63.   BNodeBase *(TreeWalkerb::* NextFptr)(); // Pointer to member functions
  64.   BNodeBase *NextPreOrder();
  65.   BNodeBase *NextInOrder();
  66.   BNodeBase *NextPostOrder();
  67.   BNodeBase *NextLvlOrder();
  68.  
  69. protected:
  70.   Queue<BNodeBase *> path; // Current path of parents through the tree
  71.   BNodeBase *root;  // Start of the tree being iterated
  72.   BNodeBase *curr;  // Current node being traversed
  73.   int state;        // Holds up or down state indication for POSTORDER
  74.   WalkOrder worder; // Enumerated type of WalkOrder 
  75. };
  76.  
  77. template<class NTYPE>
  78. class TreeWalker : private TreeWalkerb
  79. {
  80. public:
  81.   TreeWalker(NTYPE *r, WalkOrder w) : TreeWalkerb(r, w) { }
  82.   
  83. public:
  84.   void Reset() { TreeWalkerb::Reset(); }
  85.   void Reset(NTYPE *r, WalkOrder w) { TreeWalkerb::Reset(r, w); }
  86.   NTYPE *Next() { return (NTYPE *)((this->*NextFptr)()); }
  87. };
  88.  
  89. #endif // __TWALK_HPP
  90. // ----------------------------------------------------------- // 
  91. // ------------------------------- //
  92. // --------- End of File --------- //
  93. // ------------------------------- //
  94.